# -*- coding: utf-8 -*-
from django.db import models
from datetime import datetime
from basic.transmission.conf import *

class Line(models.Model):
    '''输电线路'''
    code = models.CharField('输电线路编码', max_length=20)
    name = models.CharField('输电线路名称', max_length=40)
    voltageGrade = models.CharField('电压等级', choices=LINE_VOLTAGE_GRADE,
            max_length=10)
    electricType = models.CharField('电流类型', choices=ELECTRIC_TYPE,
            max_length=20, null=True, blank=True)
    managerOrgnization = models.CharField('管理单位', max_length=40,
            null=True, blank=True)
#    stations = models.ForeignKey(Station, verbose_name='变电站', null=True, blank=True)
    fromAddress = models.CharField('起始地址', max_length=40,
            null=True, blank=True)
    toAddress = models.CharField('终止地址', max_length=40,
            null=True, blank=True)
    powerSwitch = models.CharField('电源开关', max_length=40,
            null=True, blank=True)
    loadSwitch = models.CharField('负荷开关', max_length=40,
            null=True, blank=True)
    branchLines = models.ForeignKey('self', verbose_name='分支线路',
            related_name='line', null=True, blank=True)
    maxElectric = models.CharField('最大电流', max_length=40,
            null=True, blank=True)
    designOrgnization = models.CharField('设计单位', max_length=40,
            null=True, blank=True)
    constructionOrgnization = models.CharField('施工单位', max_length=40,
            null=True, blank=True)
    buildDate = models.DateField('建设日期', default=datetime.now(),
            null=True, blank=True)
    runDate = models.DateField('运行日期', default=datetime.now(),
            null=True, blank=True)
    lineMark = models.CharField('线路色标', max_length=20,
            null=True, blank=True)
    lineGrade = models.CharField('线路等级', max_length=20,
            null=True, blank=True)
    judgementDate = models.DateField('评级日期', default=datetime.now(),
            null=True, blank=True)
    crossAddress = models.CharField('经过地区', max_length=40,
            null=True, blank=True)
    sequence = models.CharField('显示顺序', max_length=20,
            null=True, blank=True)
    equipmentWorth = models.CharField('设备价值', max_length=20,
            null=True, blank=True)
    userGrade = models.CharField('用户等级', max_length=20,
            null=True, blank=True)
    equipmentAddressID = models.CharField('设备地位编码', max_length=20,
            null=True, blank=True)
    equipmentAddress = models.CharField('设备地位', max_length=20,
            null=True, blank=True)
    assetRemarks = models.TextField('资产备注', null=True, blank=True)
    ifTJoin = models.CharField('是否T接', choices=YES_NO, max_length=20,
            null=True, blank=True)
    ifMaintain = models.CharField('是否代维', choices=YES_NO, max_length=20,
            null=True, blank=True)
    lastFixDate = models.DateField('上次维修时间', default=datetime.now(),
            null=True, blank=True)
    towers = models.ManyToManyField('Tower', verbose_name='包含杆塔',
            null=True, blank=True)
#    earthwires = models.ForeignKey('EarthWire', verbose_name='导地线',
#            null=True, blank=True)
#    crosss = models.ManyToManyField('Cross', verbose_name='交叉跨越',
#            null=True, blank=True)
    summary = models.TextField('输电线路简介', null=True, blank=True)
    
    def __unicode__(self):
        return '%s' % self.name

    class Meta:
        verbose_name_plural = verbose_name = '基础信息|输电线路(Line)'


class Tower(models.Model):
    '''杆塔'''
    code = models.CharField('杆号', max_length=20)
    type = models.CharField('型号', max_length=20, choices=TOWER_TYPE,
            null=True, blank=True)
    materialType = models.CharField('材料类型', max_length=20,
            choices=MATERIAL_TYPE, null=True, blank=True)
    forceType = models.CharField('受力类型', max_length=20,
            choices=FORCE_TYPE, null=True, blank=True)
    distance = models.FloatField('挡距', null=True, blank=True)
    fullHeight = models.FloatField('杆高(全高)', null=True, blank=True)
    huHeight = models.FloatField('呼高', null=True, blank=True)
    turn = models.FloatField('转向', null=True, blank=True)
    turnAngle = models.FloatField('转向角度', null=True, blank=True)
    sequence = models.CharField('显示顺序', max_length=20, null=True, blank=True)
    aboveSeaHeight = models.FloatField('海拔', null=True, blank=True)
    x1 = models.FloatField('X值(WGS84)', null=True, blank=True)
    y1 = models.FloatField('Y值(WGS84)', null=True, blank=True)
    z1 = models.FloatField('Z值(WGS84)', null=True, blank=True)
    x2 = models.FloatField('X值(北京1954)', null=True, blank=True)
    y2 = models.FloatField('Y值(北京1954)', null=True, blank=True)
    z2 = models.FloatField('Z值(北京1954)', null=True, blank=True)
    phasePosition = models.FloatField('相位', null=True, blank=True)
    towerTGGT = models.CharField('同杆关联杆塔', choices=TGGT,
            max_length=20, null=True, blank=True, help_text="""
                TGGT=null,为非同杆杆塔；
                TGGT=0,为同杆组中的主体；
                TGGT=1,为同杆级中的副体;
                同一同杆组中只能有一个同杆主体。
            """)
    lng = models.FloatField('经度')
    lat = models.FloatField('纬度')
    summary = models.TextField('杆塔简介', null=True, blank=True)
    
    def __unicode__(self):
        return '%s' % self.code
    
    class Meta:
        verbose_name_plural = verbose_name = '基础信息|杆塔(Tower)'


class Cross(models.Model):
    '''交叉跨越'''
    code = models.CharField('交叉跨越编码', max_length=20)
    name = models.CharField('交叉跨越名称', max_length=20)
    line = models.ForeignKey(Line, verbose_name='输电线路',
            related_name='crosss', null=True, blank=True)
    minTower = models.ForeignKey(Tower, verbose_name='小杆号',
            related_name='mintower_crosss', null=True, blank=True)
    maxTower = models.ForeignKey(Tower, verbose_name='大杆号',
            related_name='maxtower_crosss', null=True, blank=True)
    crossingObjectType = models.CharField('跨越物类型', max_length=20,
            choices=CROSSING_OBJECT_TYPE, null=True, blank=True)
    crossingObjectName = models.CharField('跨越物名称', max_length=40,
            null=True, blank=True)
    crossingObjectGrade = models.CharField('跨越物等级', max_length=20,
            choices=CROSSING_OBJECT_GRADE, null=True, blank=True)
    crossingAngle = models.FloatField('交叉角度', null=True, blank=True)
    distanceToMaxTower = models.FloatField('交叉点距大号杆水平距离(m)',
            null=True, blank=True)
    verticalDistanceToCrossingObject = models.FloatField("""
            与交叉物垂直实测距离(m)""", null=True, blank=True)
    leadSag = models.FloatField('导线弧值', null=True, blank=True)
    targetedDistanceToCrossingObject = models.FloatField("""
            跨物对地距离""", null=True, blank=True)
    measureDate = models.DateField('测量日期', default=datetime.now(),
            null=True, blank=True)
    measureTemperature = models.FloatField('测量温度', null=True, blank=True)
    verticalDistanceConvensionTo40Degree = models.FloatField("""
            换算到40℃垂直距离""", null=True, blank=True)
    verticalDistanceConvensionTo70Degree = models.FloatField("""
            换算到70℃垂直距离""", null=True, blank=True)
    ifearthWireAllowConnector = models.CharField('导地线是否允许有接头',
            choices=YES_NO, max_length=20, null=True, blank=True)
    ifcommunicationProtect = models.CharField('是否有通讯保护措施',
            choices=YES_NO, max_length=20, null=True, blank=True)
    summary = models.TextField('交叉跨越简介', null=True, blank=True)
    remarks = models.TextField('备注', null=True, blank=True)
    
    def __unicode__(self):
        return '%s' % self.name

    class Meta:
        verbose_name_plural = verbose_name = '基础信息|交叉跨越(Cross)'


class EarthWire(models.Model):
    code = models.CharField('导地线编码', max_length=20)
    name = models.CharField('导地线名称', max_length=20)
    line = models.ForeignKey(Line, verbose_name='输电线路',
            related_name='earthwires', null=True, blank=True)
    phaseDifference = models.CharField('相别', max_length=20,
            null=True, blank=True)
    installPosition = models.CharField('安装位置', max_length=20,
            null=True, blank=True)
    fromTower = models.ForeignKey(Tower, verbose_name='起始杆塔',
            related_name='fromtower_earthwires', null=True, blank=True)
    toTower = models.ForeignKey(Tower, verbose_name='终止杆塔',
            related_name='totower_earthwires', null=True, blank=True)
    earthWireModel = models.CharField('导地线型号', choices=EARTHWIRE_MODEL,
            max_length=20, null=True, blank=True)
    summary = models.TextField('导地线简介', null=True, blank=True)
    remarks = models.TextField('备注', null=True, blank=True)
    
    def __unicode__(self):
        return '%s' % self.name
    
    class Meta:
        verbose_name_plural = verbose_name = '基础信息|导地线(EarthWire)'


class Insulate(models.Model):
    '''绝缘子'''
    code = models.CharField('绝缘子编码', max_length=20)
    name = models.CharField('绝缘子名称', max_length=40)
    line = models.ForeignKey(Line, verbose_name='输电线路',
            related_name='insulates', null=True, blank=True)
    tower = models.ForeignKey(Tower, verbose_name='杆塔',
            related_name='insulates', null=True, blank=True)
    phaseDifference = models.CharField('相别', max_length=20,
            null=True, blank=True)
    type = models.CharField('绝缘类型', max_length=20,
            choices=INSULATE_TYPE, null=True, blank=True)
    installPosition = models.CharField('安装位置', max_length=20,
            null=True, blank=True)
    insulateModel = models.CharField('绝缘型号', max_length=20,
            choices=INSULATE_MODEL, null=True, blank=True)
    sheetCount = models.IntegerField('片数', null=True, blank=True)
    runDate = models.DateField('挂网日期', default=datetime.now(),
            null=True, blank=True)
    produceDate = models.DateField('生产日期', default=datetime.now(),
            null=True, blank=True)
    leaveFactoryCode = models.CharField('出厂批号', max_length=20,
            null=True, blank=True)
    sprayDate = models.DateField('喷涂日期', default=datetime.now(),
            null=True, blank=True)
    summary = models.TextField('绝缘子简介', null=True, blank=True)
    
    def __unicode__(self):
        return '%s' % self.name
    class Meta:
        verbose_name_plural = verbose_name = '基础信息|绝缘配置(Insulate)'


class TerrainInfo(models.Model):
    '''地形信息'''
    code = models.CharField('地形编码', max_length=20, unique=True)
    sequence = models.CharField('地形序号', max_length=20)
    line = models.ForeignKey(Line, verbose_name='输电线路',
            related_name='terraininfos')
    fromTower = models.ForeignKey(Tower, verbose_name='起始杆塔',
            related_name='fromtower_terraininfos')
    toTower = models.ForeignKey(Tower, verbose_name='终止杆塔',
            related_name='totower_terraininfos')
    type = models.CharField('地形分类', max_length=20,
            choices=TERRAIN_TYPE, null=True, blank=True)
    effectiveFlag = models.CharField('有效标志', max_length=20,
            choices=YES_NO, null=True, blank=True)
    remarks = models.TextField('备注', null=True, blank=True)
    createtime = models.DateTimeField('创建时间', default=datetime.now())
    updatetime = models.DateTimeField('修改时间', default=datetime.now())
    
    def __unicode__(self):
        return '%s' % self.code
    
    class Meta:
        verbose_name_plural = verbose_name = '基础信息|地形信息(TerrainInfo)'
